var template = require('../../tabbar/template.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:{},
    upgoodsNums:0,
    paisongNums:0,
    incometoday:0,//今日金额
    show: {
      basic: false,
      top: false,
      bottom: false,
      left: false,
      right: false,
      round: false,
      closeIcon: false,
      customCloseIcon: false,
      customIconPosition: false
    },
    wxlogin:true,
    information:{},//公司信息
    update_serviceTelephone:'',
    ishidden:true,
    isManage:0
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    template.tabbar("tabBar", 2, this)//0表示第一个tabbar
    let isManage =   app.globalData.isManager
    this.setData({
      isManage:isManage
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     //隐藏主页图标
     wx.hideHomeButton()
    const userinfo = wx.getStorageSync("userInfo");
    const token = wx.getStorageSync("token") || '';
   
    if (!userinfo && !token) {
      this.setData({
        wxlogin: false,
        userinfo: {},
      })
    } else {
      this.setData({
        userinfo,
      })
    }
    var that = this;
    that.getinformation()//查询公司信息
  },
  //查询公司信息
  getinformation(){
    var that = this;
    const db = wx.cloud.database()
    db.collection('information').get({
      success: function(res) {
       
        if(res.data.length >0){
          that.setData({
            information:res.data[0],
            update_serviceTelephone:res.data[0].serviceTelephone
          })
        }
      } 
    })
  },
  getUserInfo() {
    const userinfo = wx.getStorageSync("userInfo");
    this.setData({
      userinfo,
      
      wxlogin: true,
    });
  },
  //添加公告
  toNotice(){
   wx.navigateTo({
     url: '../../notice/notice',
   })
  },
  //添加员工
  addyuangong(){
    wx.navigateTo({
      url: '../../yuangong/yuangong',
    })
  },
  //添加优惠券
  addcoupons(){
   wx.navigateTo({
     url: '../../coupons/coupons',
   })
  },
//客服电话
  addPhoneNumbers(){
 
    this.setData({
      ishidden: false
    });
  },
  //取消修改客服电话
  cancel(){
    this.setData({
      ishidden: true
    });
  },
  //确认修改客服电话
  process(){
    var that = this;
    if(!that.data.update_serviceTelephone){
      wx.showModal({
        title: '提示',
        content: '请填写电话',
      })
      return
    }
    const db = wx.cloud.database()
    
    if(!that.data.information ||!that.data.information.serviceTelephone){
      db.collection('information').add({
        data:{
          serviceTelephone:that.data.update_serviceTelephone
        },
        success: function(res) {
          that.setData({
            ishidden: true
          });
          that.getinformation()//查询公司信息
        }
      })
    }else{
      
      db.collection('information').doc(that.data.information._id).update({
        data:{
          serviceTelephone:that.data.update_serviceTelephone
        },
        success: function(res) {
          that.setData({
            ishidden: true
          });
        }
      })
    }
  },
  //绑定电话输入框
  serviceTelephoneInput(e){
    this.setData({
      update_serviceTelephone:e.detail.value
    })
  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.goodsDetail.name,
      path: '/pages/goods-details/index?id=' + this.data.goodsDetail._id + '&inviter_id=' + wx.getStorageSync('uid'),
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})